package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.Chapter.ChapterVo;
import com.atguigu.eduservice.entity.Chapter.VideoVo;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-03
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        //根据课程id查询课程里的所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> listChapter = baseMapper.selectList(wrapperChapter);

        //根据课程id查询小节里的所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduVideo> listVideo = eduVideoService.list(wrapperVideo);
        //创建一个list集合，用于最终封装
        List<ChapterVo> chapterVoList = new ArrayList<>();
        //循环将章节放入最终list
        for (int i =0;i<listChapter.size();i++) {
            //每个章节
            EduChapter eduChapter = listChapter.get(i);
            //将eduChapter复制到chapterVo中
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVoList.add(chapterVo);
            //创建一个集合，用于将videolist放入到chapterVo中
            List<VideoVo> videoVoList =new ArrayList<>();
            //循环将小节放入到对应的章节中
            for (int m =0;m<listVideo.size();m++){
                EduVideo eduVideo =listVideo.get(m);
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildern(videoVoList);
        }
        return chapterVoList;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        //根据章节id查询小节表，查到就不删除。
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        //判断是否有小节
        if(count >0){
            throw  new GuiliException(20001,"有小节，需先删除完小节才能删除章节。");
        }else{//为查询出，那就是没有小节
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    @Override
    public void removeChapterCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
